package com.dongdong.zhuangji.excel.pile;

import com.dongdong.zhuangji.entity.PileData;
import com.dongdong.zhuangji.utils.DateToDayTime;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Random;

/**
 * @author dongdong 抖音号：W13883155245
 * @version 1.0
 * @date 2025/5/24
 * @description 钻孔灌注桩工程钻孔工序施工质量验收检测表
 */
public class InspectionandTestFormforConstructionQualityAcceptanceofBoringProcessinBoredPileEngineering {
    public static void main(String[] args) throws Exception {
        String excelFilePath = "D:\\devtool\\zhuangji\\src\\main\\resources\\template\\桩基质检资料筑业版.xlsx";
        String outputFilePath = "D:\\devtool\\zhuangji\\src\\main\\resources\\template\\桩基质检资料筑业版_new.xlsx";
        Workbook workbook = new XSSFWorkbook(new FileInputStream(excelFilePath));
        Sheet sheet = workbook.getSheetAt(6); // 获取已有工作表

        PileData pileData = new PileData();
        pileData.setMileage(855.123);
        pileData.setPileNumber("492-1");
        pileData.setPileBottomElevation(230.132);
        pileData.setReinforcementCageLoweringTime(new Date());
        System.out.println(new Date());
        creatsheet0(sheet, workbook,pileData);
        try (FileOutputStream out = new FileOutputStream(outputFilePath)) {
            workbook.write(out);
        }
    }

    public static void creatsheet0(Sheet sheet, Workbook workbook, PileData pileData) {
        // 修改目标单元格（示例为10 B=9 1单元格）
        Row row = sheet.getRow(9);
        Cell cell = row.getCell(1);
        String originalText = cell.getStringCellValue();
        String targetText = "临巴堤防工程-土石方开挖与地基处理工程-(K0+000.0)000-0#桩钢筋笼制作"; // 要替换的文本
        String replaceText = "临巴堤防工程-土石方开挖与地基处理工程-(K0+"+pileData.getMileage()+")"+pileData.getPileNumber()+"#桩钢筋笼制作";
        // 创建基础富文本
        RichTextString richText = workbook.getCreationHelper().createRichTextString(originalText.replace(targetText, replaceText));
        // 获取当前文本长度作为起始位置

        // 通过重新创建包含追加内容的富文本
        String combinedText = richText.getString();
        richText = workbook.getCreationHelper().createRichTextString(combinedText);
        // 对新内容应用特殊格式
        // richText.applyFont(startPos, combinedText.length(), redFont);

        // 创建格式字体
        Font redFont = workbook.createFont();

        redFont.setFontName("宋体");
        redFont.setFontHeight((short) 220);
        int start=0;
        int end=richText.length();

        richText.applyFont(start, end, redFont);//起点，止点，样式

        redFont.setColor(IndexedColors.RED.getIndex());
        redFont.setUnderline(Font.U_SINGLE);

        System.out.println(combinedText);

        start=combinedText.indexOf(replaceText);
        end=replaceText.length()+start;

        richText.applyFont(start, end, redFont);//起点，止点，样式
        cell.setCellValue(richText);


        //--------------------------------------------------------
        // 修改目标单元格（示例为10 B=9 1单元格）
        row = sheet.getRow(5);
        cell = row.getCell(1);
        originalText = cell.getStringCellValue();
        targetText = "孔底高程000.000m"; // 要替换的文本
        replaceText = "孔底高程"+pileData.getPileBottomElevation()+"m";
        // 创建基础富文本
        richText = workbook.getCreationHelper().createRichTextString(originalText.replace(targetText, replaceText));
        // 获取当前文本长度作为起始位置

        // 通过重新创建包含追加内容的富文本
        combinedText = richText.getString();
        richText = workbook.getCreationHelper().createRichTextString(combinedText);
        // 对新内容应用特殊格式
        // richText.applyFont(startPos, combinedText.length(), redFont);


        redFont.setFontName("宋体");
        redFont.setColor(IndexedColors.BLACK.getIndex());
        redFont.setUnderline(Font.U_NONE);
        redFont.setFontHeight((short) 220);
        start=0;
        end=richText.length();
        richText.applyFont(start, end, redFont);//起点，止点，样式


        // 创建格式字体
        redFont.setColor(IndexedColors.RED.getIndex());
        redFont.setUnderline(Font.U_NONE);

        System.out.println(combinedText);

        start=combinedText.indexOf(replaceText)+4;
        end=replaceText.length()+start-5;

        richText.applyFont(start, end, redFont);//起点，止点，样式
        cell.setCellValue(richText);

        //---------------------------------------------
        // 修改目标单元格（示例为10 B=9 1单元格）
        row = sheet.getRow(3);
        cell = row.getCell(1);
        cell.setCellValue("(K0+"+pileData.getMileage()+")"+pileData.getPileNumber()+"#桩");

        cell = row.getCell(4);
        cell.setCellValue( new DateToDayTime().toLoaclDays(pileData.getReinforcementCageLoweringTime())+"" );
    }
    private int between(int min,int max){
        Random random = new Random();
        // 生成 -10 到 10 之间的随机整数（包含 -10 和 10）
        return random.nextInt(max - min + 1) + min;
    }
}
